import { queryRepositoryList,  actionRepositoryAdd, actionRepositoryrExport,actionRepositoryDel,actionRepositoryMove,
} from '../services/repository';

export default {
  namespace: 'repository',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    loading: {
      reslist: true,
      model_submit:false,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_repository_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryRepositoryList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 批量删除操作
    *action_repository_del({ payload }, { call, put }) {
      const response = yield call(actionRepositoryDel, payload);
    },
    // 导出
    *action_repository_export({ payload }, { call, put }) {
      const response = yield call(actionRepositoryrExport, payload);
    },

    // 移动
    *action_repository_move({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionRepositoryMove,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 新增文档
    *add_repository({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionRepositoryAdd,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },


    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendDataRemind(state, action) {
      return {
        ...state,
        remindData: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        loading:{}
      };
    },
  },

};
